package com.bw.study.glide;

import android.content.Context;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * 替换掉原有的glide框架的httpurlconnection加载方式，修改为okhttp加载，
 * 项目中联网请求都基于okhttp框架完成，可在okhttp基础上做统一管理
 * */
@GlideModule
public class OkGlideModule extends AppGlideModule {

    @Override
    public void registerComponents(@NonNull Context context,
                                   @NonNull Glide glide, @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
        //okhttpclient配置
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(30, TimeUnit.SECONDS);
        builder.writeTimeout(30,TimeUnit.SECONDS);
        builder.readTimeout(30,TimeUnit.SECONDS);
        //构建okhttpclient对象
        OkHttpClient client = builder.build();
        //替换掉原有加载
        registry.replace(GlideUrl.class,
                InputStream.class, new OkHttpUrlLoader.Factory(client));
    }
    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }
}
